import pandas as pd
import numpy as np
import scipy.interpolate as spi
import matplotlib.pyplot as plt
from utils3 import produce_x_v7, cubic
import os
import shutil

save_csv_dir = r"D:\data_cmp\airfoil_csv_op\airfoil_csv_op_11_leading"
save_pic_dir = r"D:\data_cmp\airfoil_csv_op\airfoil_csv_op_11_rmleading_show"

def func(file_name):
    n = 151

    # 插值前
    file_path1 = os.path.join(r"D:\data_cmp\airfoil_csv_op\airfoil_csv_op_11_bspline_pre", file_name)
    ordinates1 = pd.read_csv(file_path1, index_col=None, header=0).to_numpy()

    # 插值后
    file_path2 = os.path.join(r"D:\data_cmp\airfoil_csv_op\airfoil_csv_op_11_rmleading", file_name)
    ordinates2 = pd.read_csv(file_path2, index_col=None, header=0).to_numpy()

    # ******************************************
    # 获取上下表面
    # ordinates1_tmp = ordinates1.copy()[::-1]
    #
    # # 前缘顶点
    # key = np.argmin(ordinates1_tmp[:, 0])
    #
    # 下表面关于y轴作对称
    # ordinates1_tmp[0:key, 0] = ordinates1_tmp[0:key, 0] * (-1)
    #
    # # 插值（方法一）
    # ordinates_CbSp = np.empty(shape=(2 * n - 1, 2))
    # x_low = produce_x_v7(N=n)[::-1] * (-1)
    # x_up = produce_x_v7(N=n)[1::]
    # x = np.concatenate([x_low, x_up], axis=0)
    #
    # ipo3_x = spi.splrep(ordinates1_tmp[:, 0], ordinates1_tmp[:, 1], k=3)  # 样本点导入，生成参数
    # ordinates_CbSp[:, 1] = spi.splev(x, ipo3_x)  # 根据观测点和样条参数，生成插值
    # ordinates_CbSp[:, 0] = x
    #
    # key = n
    # # 插值后的下表面关于y轴作对称和逆序
    # ordinates_CbSp[0:key, 0] = ordinates_CbSp[0:key, 0] * (-1)
    # ******************************************

    # 插值后1（方法二）
    # 获取上下表面
    ordinates1_tmp = ordinates1.copy()

    # 前缘顶点
    key = np.argmin(ordinates1_tmp[:, 0])
    # 上表面坐标点
    ordinates_CbSp_up = ordinates1_tmp[0:key + 1]
    ordinates_CbSp_up = ordinates_CbSp_up[::-1]

    # 下表面坐标点
    ordinates_CbSp_low = ordinates1_tmp[key::]

    # 插值
    x = produce_x_v7(N=n)
    ordinates_CbSp = np.empty(shape=(2 * n - 1, 2))

    # 上表面
    # ordinates_CbSp_up = np.concatenate([np.asarray([[-2e-7, -0.0000001]]),
    #                                      ordinates_CbSp_up], axis=0)
    ipo3_x = spi.splrep(ordinates_CbSp_up[:, 0], ordinates_CbSp_up[:, 1], k=3)  # 样本点导入，生成参数
    ordinates_CbSp[0:n, 1] = spi.splev(x, ipo3_x)[::-1]  # 根据观测点和样条参数，生成插值
    ordinates_CbSp[0:n, 0] = x[::-1]

    # 下表面
    # ordinates_CbSp_low = np.concatenate([np.asarray([[-2e-7, 0.00000001]]),
    #                                      ordinates_CbSp_low], axis=0)
    ipo3_x = spi.splrep(ordinates_CbSp_low[:, 0], ordinates_CbSp_low[:, 1], k=3)  # 样本点导入，生成参数
    ordinates_CbSp[n-1::, 1] = spi.splev(x, ipo3_x)  # 根据观测点和样条参数，生成插值
    ordinates_CbSp[n-1::, 0] = x

    # save as csv
    pd.DataFrame(ordinates_CbSp).to_csv(os.path.join(save_csv_dir, file), index=None, header=['x', 'y'])

    # show
    plt.plot([0, 0.01], [0, 0], '--')
    plt.plot([0, 0], [0.001, -0.01], '--')
    plt.scatter(ordinates1[:, 0], ordinates1[:, 1], c='grey', s=5, label="插值前")
    plt.scatter(ordinates2[:, 0], ordinates2[:, 1], c='red', s=5, label="插值后1")
    plt.scatter(ordinates_CbSp[:, 0], ordinates_CbSp[:, 1], c='blue', s=5, label="插值后2")
    plt.xlim((-0.000001, 0.00001))  # 设置横坐标范围
    plt.ylim((-0.0008, 0.0008))  # 设置纵坐标范围
    # plt.show()
    plt.savefig(os.path.join(save_pic_dir, file_name[:-4]+".jpg"), dpi=600)
    plt.clf()

pro_dir = r"D:\data_cmp\airfoil_csv_op\airfoil_csv_op_11_rmleading"
for file in os.listdir(pro_dir):
    print(file)
    try:
        func(file)
        # shutil.move(os.path.join(r"D:\data_cmp\airfoil_csv_op\airfoil_csv_op_11_leading", file),
        #             os.path.join(r"D:\data_cmp\airfoil_csv_op\airfoil_csv_op_11_leading1", file))
        # os.remove(os.path.join(pro_dir, file))
    except:
        print("error:", file)

# ag45c03
# e61
# e398
# e407



